package cn.iocoder.yudao.module.wms.controller.admin.stock;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.stock.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.stock.StockDO;
import cn.iocoder.yudao.module.wms.service.stock.StockService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 库存汇总")
@RestController
@RequestMapping("/wms/stock")
@Validated
public class StockController {

    @Resource
    private StockService stockService;

    @PostMapping("/create")
    @Operation(summary = "创建库存汇总")
    @PreAuthorize("@ss.hasPermission('wms:stock:create')")
    public CommonResult<Long> createStock(@Valid @RequestBody StockSaveReqVO createReqVO) {
        return success(stockService.createStock(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存汇总")
    @PreAuthorize("@ss.hasPermission('wms:stock:update')")
    public CommonResult<Boolean> updateStock(@Valid @RequestBody StockSaveReqVO updateReqVO) {
        stockService.updateStock(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存汇总")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:stock:delete')")
    public CommonResult<Boolean> deleteStock(@RequestParam("id") Long id) {
        stockService.deleteStock(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存汇总")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:stock:query')")
    public CommonResult<StockRespVO> getStock(@RequestParam("id") Long id) {
        StockDO stock = stockService.getStock(id);
        return success(BeanUtils.toBean(stock, StockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存汇总分页")
    @PreAuthorize("@ss.hasPermission('wms:stock:query')")
    public CommonResult<PageResult<StockRespVO>> getStockPage(@Valid StockPageReqVO pageReqVO) {
        PageResult<StockDO> pageResult = stockService.getStockPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存汇总 Excel")
    @PreAuthorize("@ss.hasPermission('wms:stock:export')")
    @OperateLog(type = EXPORT)
    public void exportStockExcel(@Valid StockPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockDO> list = stockService.getStockPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存汇总.xls", "数据", StockRespVO.class,
                        BeanUtils.toBean(list, StockRespVO.class));
    }

}